#include <string.h>
#include <time.h>
#include <stdio.h>

#include "cJSON.h"
#include "get_ID.h"
#include "get_temp.h"
#include "get_cJSON.h"

int get_cJSON(char **out)
{
	cJSON                *root;
	struct               tm *info;
	int                  rv=-1;
	time_t               rawtime;
	char                 buf[52]={};

	struct              rpi_data_s  rpi_data;
	struct              rpi_ID_s    rpi_ID;
	typedef struct      cJSON       cJSON;


	memset(&rpi_data, 0, sizeof(rpi_data));
	memset(&rpi_ID, 0, sizeof(rpi_ID));

	rv = get_ID(&rpi_ID);
	if(rv)
	{
		printf("get_ID failure:%s\n");
		goto clean;
	}
	printf("get_ID successful!\n");

	rv = get_temperature(&rpi_data);
	if(rv)
	{
		printf("get_temp failure:%s\n");
		goto clean;
	}
	printf("get_temp successful!\n");

	time( &rawtime );
	info =localtime( &rawtime );

	root=cJSON_CreateObject();
	cJSON_AddItemToObject(root, "temperature",cJSON_CreateNumber(rpi_data.temp));
	cJSON_AddItemToObject(root, "Rpi",cJSON_CreateString(rpi_ID.ID));
	cJSON_AddItemToObject(root, "time",cJSON_CreateString(asctime(info)));

	*out = cJSON_Print(root);
	printf("out:%s\n",*out);

	cJSON_Delete(root);

clean:
	return 0;
}

